/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package icexam.dao;

import icexam.user.UserBean;
import icexam.util.PswUtil;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ethan
 */
public class PSWDao {

    public boolean checkPSW(String psw) {
//        String tmp = PswUtil.getBASE64(user.getPassword());
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("select * from admin where id=0");
            ResultSet rs = statement.executeQuery();
            String tmp = null;
            if(rs.next()){
                tmp = rs.getString("password");
                if(psw.equals(PswUtil.getFromBASE64(tmp)))
                    return true;   
            }
            else
                return false;
        } catch (SQLException ex) {
            Logger.getLogger(PSWDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
        
    }

    public void changePSW(String psw) {
        psw = PswUtil.getBASE64(psw);
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("update admin set password = ? where id=0");
            statement.setString(1, psw);
            statement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(PSWDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static void main(String[] args){
//        try {
//            Connection conn = DBConnect.getConnection();
//            PreparedStatement statement = null;
//                statement = conn.prepareStatement("select now()");
//            ResultSet rs = statement.executeQuery();
//            while(rs.next())
//            System.out.println(rs.getString(1));
//        } catch (SQLException ex) {
//            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
//        }
        System.out.println(Math.round(Math.random()*9));

    }
    

   

    public String[] getLoginPsw() {
        String[] arrayStr = new String[16];
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("select * from admin where id!=0 order by id");
            ResultSet rs = statement.executeQuery();
            String tmp = null;
            int i =0;
            while(rs.next()){
                tmp = rs.getString("userright");
                arrayStr[i++] = PswUtil.getFromBASE64(tmp);
            }
            return arrayStr;
        } catch (SQLException ex) {
            Logger.getLogger(PSWDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

    public void changeUserRightPSW(String psw) {
        psw = PswUtil.getBASE64(psw);
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("update admin set userright = ? where id=0");
            statement.setString(1, psw);
            statement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(PSWDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean checkUserRight(String psw) {
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("select * from admin where id!=0");
            ResultSet rs = statement.executeQuery();
            String tmp = null;
            while(rs.next()){
                tmp = rs.getString("userright");
                if(psw.equals(PswUtil.getFromBASE64(tmp)))
                    return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(PSWDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public void changeUserRightPSW(String psw, int i) {
        psw = PswUtil.getBASE64(psw);
        try {
            Connection conn = DBConnect.getConnection();
            PreparedStatement statement = conn.prepareStatement("update admin set userright = ? where id = ?");
            statement.setString(1, psw);
            statement.setInt(2, i);
            statement.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(PSWDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
